/**
 *
 * AtJava source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to obinary.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 Atjava.com Ltd. (http://www.atjava.com) All rights reserved.
 *
 */
package com.atjava.jcrt.core.query;

import java.util.ArrayList;
import java.util.List;

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.UnsupportedRepositoryOperationException;
import javax.jcr.query.InvalidQueryException;
import javax.jcr.query.QueryManager;

import org.apache.log4j.Logger;

import com.atjava.jcrt.BaseInfo;
import com.atjava.jcrt.core.ContentData;
import com.atjava.jcrt.core.ItemType;
import com.atjava.jcrt.core.NodeData;
import com.atjava.jcrt.core.transaction.XmlTransactionException;
import com.atjava.jcrt.core.version.VersionImpl;

/**
 * 
 * @author biggie
 * @version $Revision: 1.8 $ $Date: 2008/07/17 04:35:08 $
 */
public class QueryImpl implements Query {
	
	private QueryManager queryManager = null;

	private BaseInfo baseInfo = null;

	private String cons = null;
	
	private static Logger log = Logger.getLogger(VersionImpl.class.getName());

	private List<Object> results = null;
	public QueryImpl(QueryManager queryManager, BaseInfo baseInfo,String cons) throws QueryException{
		
		this.queryManager = queryManager;
		this.baseInfo = baseInfo;
		
		if(cons != null){
			this.cons = cons;
			this.init();
		}
	}
	public List<Object> getDefaultResults() {
		// TODO Auto-generated method stub
		return this.results;
	}

	private void init() throws QueryException{
		try {
			log.debug("QueryImpl init() cons = "+this.cons);
			results = new ArrayList<Object>();
			javax.jcr.query.Query query = this.queryManager.createQuery(this.cons,javax.jcr.query.Query.XPATH);
			NodeIterator ni = query.execute().getNodes();
			log.debug("QueryImpl init() NodeIterator.getSize = "+ni.getSize());
			
			String ns = baseInfo.getXmlJcrtInfoDetail().getNamespace();
			while (ni.hasNext()) {
				BaseInfo info = (BaseInfo) baseInfo.clone();
				Node nv = ni.nextNode();
				//****************************************
				//***this code is so bad,temporarily code 
				if(nv.getPath().indexOf("/"+ns+":") != -1){
					if(info.getNodeType().equals(ItemType.CONTENT)){
						ContentData cdata = new ContentData(nv);
						info = (BaseInfo) cdata.changeToInfoClass(info);
						if(baseInfo.getClassName().equals(info.getClassName())){
							info.setRelPath(nv.getPath());
							info.setNode(nv);
							results.add(info);
						}
					}else{
						NodeData cdata = new NodeData(nv);
						info = (BaseInfo) cdata.changeToInfoClass(info);
						if(baseInfo.getClassName().equals(info.getClassName())){
							info.setRelPath(nv.getPath());
							info.setNode(nv);
							results.add(info);
						}
					}	
				}
			}
		} catch (CloneNotSupportedException e) {
			throw new QueryException("CloneNotSupportedException", e);
		} catch (UnsupportedRepositoryOperationException e) {
			throw new QueryException("UnsupportedRepositoryOperationException", e);
		} catch (RepositoryException e) {
			throw new QueryException("RepositoryException", e);
		} catch (XmlTransactionException e) {
			throw new QueryException("XmlTransactionException", e);
		}
	}
	public List<Object> getResultsByQL(String consQL)  throws QueryException{
		
		try {
			log.debug("QueryImpl getResultsByQL() cons = "+consQL);
			results = new ArrayList<Object>();
			javax.jcr.query.Query query = 
				this.queryManager.createQuery(consQL,javax.jcr.query.Query.SQL);
			NodeIterator ni = query.execute().getNodes();
			log.debug("QueryImpl getResultsByQL() NodeIterator.getSize = "+ni.getSize());
			String ns = baseInfo.getXmlJcrtInfoDetail().getNamespace();
			while (ni.hasNext()) {
				Node nv = ni.nextNode();
				//****************************************
				//***this code is so bad,temporarily code 
				if(nv.getPath().indexOf("/"+ns+":") != -1){
					BaseInfo info = (BaseInfo) this.baseInfo.clone();
					ContentData cdata = new ContentData(nv);
					info = (BaseInfo) cdata.changeToInfoClass(info);
					info.setNode(nv);
					results.add(info);
				}
			}
		} catch (InvalidQueryException e) {
			throw new QueryException("InvalidQueryException", e);
		} catch (RepositoryException e) {
			throw new QueryException("RepositoryException", e);
		} catch (CloneNotSupportedException e) {
			throw new QueryException("CloneNotSupportedException", e);
		} catch (XmlTransactionException e) {
			throw new QueryException("XmlTransactionException", e);
		}
		return results;
	}

}
